package prgechecsimpla;

import prgechecsmodel.IMove;

public final class Move implements Comparable<IMove>, IMove {

    public static String inputNotation(int i) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    int from, to, promote, bits;
    int score = 0;

    public Move(int from, int to, int promote, int bits) {
        this.from = from;
        this.to = to;
        this.promote = promote;
        this.bits = bits;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int i) {
        score = i;
    }

    @Override
    public int hashCode() {
        return from + (to << 8) + (promote << 16);
    }

    @Override
    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    public boolean equals(Object o) {
        Move m = (Move) o;
        return (m.from == from && m.to == to && m.promote == promote);
    }

    @Override
    public int compareTo(IMove o) {
        Move m = (Move) o;
        int mScore = m.getScore();
        if (score < mScore) {
            return 1;
        }
        if (score > mScore) {
            return -1;
        }
        int mHashCode = m.hashCode();
        int hash = hashCode();
        if (hash > mHashCode) {
            return 1;
        }
        if (hash < mHashCode) {
            return -1;
        }
        return 0;
    }

    @Override
    public String toString() {
        char c;
        StringBuffer sb = new StringBuffer();

        if ((bits & 32) != 0) {
            switch (promote) {
                case Board.KNIGHT:
                    c = 'n';
                    break;
                case Board.BISHOP:
                    c = 'b';
                    break;
                case Board.ROOK:
                    c = 'r';
                    break;
                default:
                    c = 'q';
                    break;
            }
            sb.append((char) (Board.COL(from) + 'a'));
            sb.append(8 - Board.ROW(from));
            sb.append((char) (Board.COL(to) + 'a'));
            sb.append(8 - Board.ROW(to));
            sb.append(c);
        } else {
            sb.append((char) (Board.COL(from) + 'a'));
            sb.append(8 - Board.ROW(from));
            sb.append((char) (Board.COL(to) + 'a'));
            sb.append(8 - Board.ROW(to));
        }
        return sb.toString();
    }

    @Override
    public String notation(int move) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void positionToString(int position, StringBuilder buffer) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
